package main

import (
	"./spider"
	"fmt"
	"time"
)

var (
	sleep          = time.Second * 3 // 下次请求延时时间
	searchPageSize = 25              // 搜索页页面大小
	top250PageSize = 25              // top 250 页面大小
)

func main() {
	// getTop250()
}

func getSearch() {
	start := 0
	pageSize := searchPageSize
	subjectIDs := []string{}
	isEnd := false
	for !isEnd {
		link := "https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=" + string(pageSize) + "&page_start=" + string(start)
		html := spider.HttpClient.GetStr(link)
		subjectIDs = spider.GetSearchList(html)
		if len(subjectIDs) < pageSize {
			isEnd = true
		}
		start += pageSize
		fmt.Printf("前 %d 条数据...\n", start)
		time.Sleep(sleep)
	}
	getMovieDetail(subjectIDs)
}

func getTop250() {
	start := 0
	pageSize := top250PageSize
	subjectIDs := []string{}
	for start < 250 {
		link := fmt.Sprintf("https://movie.douban.com/top250?start=%d", start)
		html, ok := spider.HttpClient.Get(link)
		if ok {
			subjectIDs = append(subjectIDs, spider.GetMovieList(html)...)
		}
		start += pageSize
		fmt.Printf("前 %d 条数据...\n", start)
		time.Sleep(sleep)
	}
	getMovieDetail(subjectIDs)
	// subjectID := "27092785"
	// link := fmt.Sprintf("https://movie.douban.com/subject/%s", subjectID)
	// html, ok := spider.HttpClient.Get(link)
	// if ok {
	// 	movie, err := spider.GetMovie(html)
	// 	if err != nil {
	// 		fmt.Println("===========  GetMovie ===========", err)
	// 	} else {
	// 		movie.SubjectID = subjectID
	// 		movie.Link = link
	// 		sql := movie.ToInsertSql()

	// 		fmt.Println(spider.InsertData(sql))
	// 	}
	// }
}

func getMovieDetail(subjectIDs []string) {
	for index, subjectID := range subjectIDs {
		fmt.Printf("第 %d 条详情, id: %s ...\n", index, subjectID)
		link := fmt.Sprintf("https://movie.douban.com/subject/%s", subjectID)
		html, ok := spider.HttpClient.Get(link)
		if ok {
			movie, err := spider.GetMovie(html)
			if err != nil {
				fmt.Println("===========  GetMovie ===========", err)
			} else {
				movie.SubjectID = subjectID
				movie.Link = link
				sql := movie.ToInsertSql()
				err := spider.InsertData(sql)
				if err == nil {
					fmt.Println("成功")
				}
			}
		}
		time.Sleep(sleep)
	}
}

func init() {
	fmt.Println("welcome to go-movie-spider")

}
